package cn.wit.contest.controller;


import cn.wit.contest.condition.ContestCondition;
import cn.wit.utils.pojo.Contest;
import cn.wit.contest.service.ContestService;
import cn.wit.utils.response.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/contest")
@Api(tags = "竞赛")
public class ContestController {
    private Logger logger = LoggerFactory.getLogger(ContestController.class);
    @Autowired
    private ContestService contestService;

    //新增一条竞赛记录
    @PostMapping("/insertContest")
    @ApiOperation("新增一条竞赛记录")
    public Result insertContest(@RequestBody Contest contest) {
        return this.contestService.insert(contest);
    }

    //根据ID删除一条记录
    @DeleteMapping("/deleteContest/{id}")
    @ApiOperation("根据ID删除一条竞赛记录")
    public Result deleteContestById(@PathVariable("id") int id) {
        return this.contestService.deleteById(id);
    }

    //根据ID更新记录
    @PutMapping("/updateContest")
    @ApiOperation("根据ID更新竞赛记录")
    public Result updateContest(@RequestBody Contest contest) {
        return this.contestService.updateById(contest);
    }

    @PostMapping("/selectById")
    @ApiOperation("根据id查询竞赛记录")
    public Result selectById(Integer id){
        return this.contestService.selectById(id);
    }

    //查询不同状态的竞赛信息(全部、即将发布、进行中、往期比赛)
    @GetMapping("/queryContest/{num}/{size}")
    public Result queryContest(@PathVariable("num") int num,
                               @PathVariable("size") int size,
                               ContestCondition condition) {
        IPage<Contest> page = new Page<>(num, size);//构造IPage对象
        return this.contestService.queryContest(page,condition);
    }

    @GetMapping("/download/contest/template")
    @ApiOperation("下载excel模板文件")
    public String downloadExcel(){
        return "http://localhost:9001/contest.xlsx";//返回前端一个下载链接
    }

    @PostMapping("/importExcel")
    @ApiOperation(value = "Excel导入竞赛信息")
    public Result excelContestInfo(@RequestParam("file")MultipartFile file){
        return this.contestService.excelImport(file);
    }
}
